package com.spic.business.train.prepare.controller;

import com.spic.business.train.examination.domain.TrainPaper;
import com.spic.business.train.prepare.dto.ExamDetailRespDTO;
import com.spic.business.train.prepare.dto.PaperAnSwerDTO;
import com.spic.business.train.prepare.service.ITrainPrepareService;
import com.spic.business.train.questions.domain.TrainQuestions;
import com.spic.common.core.web.controller.BaseController;
import com.spic.common.core.web.domain.AjaxResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 开始考试Controller
 *
 * @author yangjie
 * @date 2021-10-21
 */
@RestController
@RequestMapping("/train/prepare")
public class TrainPrepareController extends BaseController
{
    @Autowired
    private ITrainPrepareService trainPrepareService;

    /**
     * 查询题库管理列表
     */
    @GetMapping("/questionslist/{examId}/{userId}")
    public AjaxResult questionsList(@PathVariable("examId")Long examId,@PathVariable("userId") Long userId)
    {
        List<TrainQuestions> list = trainPrepareService.selectTrainQuestionsList(examId,userId);
        return AjaxResult.success(list);
    }

    /**
     * 获取试卷 详细信息
     */
    @GetMapping("/paperlist/{examId}")
    public AjaxResult paperlistByExamId(@PathVariable("examId")Long examId)
    {
        List<TrainPaper> list = trainPrepareService.selectPaperlistByExamId(examId);
        return AjaxResult.success(list);
    }

    @GetMapping("/paperQuestions/{examId}")
    public AjaxResult paperQuestionsByExamId(@PathVariable("examId")Long examId)
    {
        Map<String,Object> map = trainPrepareService.selectPaperQuestionsByExamId(examId);
        return AjaxResult.success(map);
    }
    /**
     * 获取题库管理详细信息
     */
    @GetMapping(value = "/questions/{id}")
    public AjaxResult questionsByid(@PathVariable("id") String id)
    {
        TrainQuestions trainQuestions = trainPrepareService.selectTrainQuestionsById(id);
        return AjaxResult.success(trainQuestions);
    }

    /**
     * 根据考试id sort 查询
     */
    @GetMapping(value = "/selectExamAndSort")
    public AjaxResult questionsByExamAndSort(PaperAnSwerDTO paperAnSwerDTO)
    {
//        SpringUtils.getBean(RedisService.class).setCacheObject(getCacheKey(), deptDTOS);
        PaperAnSwerDTO paperAnSwer = trainPrepareService.selectquestionsByExamAndSort(paperAnSwerDTO);
        return AjaxResult.success(paperAnSwer);
    }
    //标题详情
    @GetMapping(value = "/paper-detail/{examId}")
    public AjaxResult paperDetail(@PathVariable("examId") Long examId) {
        ExamDetailRespDTO respDTO = trainPrepareService.paperDetail(examId);
        return AjaxResult.success(respDTO);
    }

    /**
     * 根据考试id sort 更新考生答题
     */
    @PutMapping(value = "/updatePaperByExamAndSort")
    public AjaxResult updatePaperByExamAndSort(@RequestBody PaperAnSwerDTO paperAnSwerDTO)
    {
       trainPrepareService.updatePaperByExamAndSort(paperAnSwerDTO);
        return AjaxResult.success();
    }

    /**
     * 判分
     * @param trainPaper 试卷
     * @return
     */
    @PostMapping(value = "/criticalscore")
    public AjaxResult criticalScore(@RequestBody TrainPaper trainPaper){
        List<Map<String,Object>> scoreDetails = trainPrepareService.criticalScore(trainPaper);
        return AjaxResult.success(scoreDetails);
    }

    //获取考试结束试卷
    @GetMapping("/examResultlist/{examId}/{userId}")
    public AjaxResult examResultlist(@PathVariable("examId")Long examId,@PathVariable("userId")Long userId)
    {
        HashMap<String,Object> list = trainPrepareService.selectTrainExamResultList(examId,userId);
        return AjaxResult.success(list);
    }

    //
    @GetMapping("/examFinshInfo/{examId}/{userId}")
    public AjaxResult trainExamUserStateByFinsh (@PathVariable("examId")Long examId,@PathVariable("userId")Long userId){
        return AjaxResult.success(trainPrepareService.trainExamUserStateByFinsh(examId,userId));
    }


}
